<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24"></a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <div style="margin-bottom:20px">
        <label>总设备数：{{this.ipagination.total}}个</label>
      </div>
      <label>显示列：</label>
      <a-select mode="multiple" placeholder="请选择展示列" :value="selectedItems" style="width: 100%;margin-bottom:20px;" @change="handleChange">
        <a-select-option v-for="(item,index) in deepColunms" :key="index" :value="item.dataIndex">{{ item.title }}</a-select-option>
      </a-select>
      <a-button v-if="false" @click="test" type="primary" icon="plus">测试</a-button>
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('设备信息')">导出</a-button>
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete"/>删除
          </a-menu-item>
          <a-menu-item key="2" @click="batchOutIn(1)">
            <a-icon type="logout"/>批量出库
          </a-menu-item>
          <a-menu-item key="3" @click="batchOutIn(0)">
            <a-icon type="login"/>批量入库
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <div slot="pic" slot-scope="text">
          <img @click="success(text)" style="width: 50px;height:50px" :src="GetQrCode(text)">
        </div>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt style="max-width:80px;font-size: 12px;font-style: italic;">
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">下载</a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a v-if="record.isOut!==1" @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical"/>
          <a @click="WarehouseEdit(record,1)">出库</a>
          <a-divider type="vertical"/>
          <a @click="WarehouseEdit(record,0)">入库</a>
          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">更多
              <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="downloadFile(record,1)">下载使用说明书</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="downloadFile(record,2)">下载操作规范</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="downloadFile(record,3)">下载合格证书</a>
              </a-menu-item>
              <a-menu-item v-if="record.isOut!==1">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <device-info-modal ref="modalForm" @ok="modalFormOk"></device-info-modal>
    <a-modal v-model="visible" ok-text="打印" @ok="hideModal">
      <div id="print" style="text-align:center">
        <img style="width: 400px;height: 400px;margin: 0 auto;" :src="imgUrl">
      </div>
    </a-modal>
  </a-card>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import DeviceInfoModal from './modules/DeviceInfoModal'
import { Modal } from 'ant-design-vue'
import QRCode from 'qrcodejs2'
import axios from 'axios'

export default {
  name: 'DeviceInfoList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    DeviceInfoModal
  },
  data() {
    return {
      selectedItems: [],
      visible: false,
      imgUrl: '',
      description: '设备信息管理页面',
      deepColunms: [],
      // 表头
      columns: [
        // {
        //   title: '#',
        //   dataIndex: '',
        //   key: 'rowIndex',
        //   width: 60,
        //   align: 'center',
        //   customRender: function(t, r, index) {
        //     return parseInt(index) + 1
        //   }
        // },
        {
          title: '设备编码',
          align: 'center',
          dataIndex: 'deviceCode',
          visible: false
        },
        {
          title: '设备二维码',
          align: 'center',
          dataIndex: 'id',
          scopedSlots: { customRender: 'pic' }
        },
        {
          title: '设备名称',
          align: 'center',
          dataIndex: 'deviceName'
        },
        {
          title: '型号',
          align: 'center',
          dataIndex: 'deviceType'
        },
        {
          title: '购置单位',
          align: 'center',
          dataIndex: 'buyDepart_dictText'
        },
        {
          title: '购置时间',
          align: 'center',
          dataIndex: 'buyDate',
          customRender: function(text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          }
        },
        {
          title: '检验时间',
          align: 'center',
          dataIndex: 'warnDate',
          customRender: function(text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          }
        },
        {
          title: '报废时间',
          align: 'center',
          dataIndex: 'scrapDate',
          customRender: function(text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          }
        },
        {
          title: '使用单位',
          align: 'center',
          dataIndex: 'useDepart_dictText'
        },
        {
          title: '工器具分类',
          align: 'center',
          dataIndex: 'deviceSort_dictText',
          filters: [{ text: '施工工器具	', value: '1' }, { text: '安全工器具', value: '2' }]
        },
        {
          title: '是否租赁',
          align: 'center',
          dataIndex: 'isRent_dictText'
        },
        {
          title: '是否出库',
          align: 'center',
          dataIndex: 'isOut_dictText'
        },
        {
          title: '最大出力',
          align: 'center',
          dataIndex: 'maxOutput'
        },
        {
          title: '保管人',
          align: 'center',
          dataIndex: 'deviceKeeper'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'deviceNote'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/device/deviceInfo/list',
        delete: '/device/deviceInfo/delete',
        deleteBatch: '/device/deviceInfo/deleteBatch',
        exportXlsUrl: '/device/deviceInfo/exportXls',
        importExcelUrl: 'device/deviceInfo/importExcel',
        getQrCode: 'device/deviceInfo/getQrCode',
        downloadFile: 'device/deviceInfo/downloadMoreFile',
        warehouseEdit: 'device/deviceInfo/warehouseEdit',
        batchwarehouseEdit: 'device/deviceInfo/batchwarehouseEdit'
      },
      dictOptions: {}
    }
  },
  created() {},
  mounted() {
    this.deepColunms = JSON.parse(JSON.stringify(this.columns))
    this.selectedItems = this.deepColunms.map(item => {
      return item.dataIndex
    })
    console.log('123', this.deepColunms)
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    filteredOptions() {
      return OPTIONS.filter(o => !this.selectedItems.includes(o))
    }
  },
  methods: {
    handleChange(selectedItems) {
      console.log(selectedItems)
      this.columns = this.deepColunms.filter(item => {
        return selectedItems.some(sel => {
          return item.dataIndex === sel
        })
      })
      console.log(this.columns)
      this.selectedItems = selectedItems
    },
    test() {
      console.log('this.ipagination', this.ipagination)
    },
    batchOutIn: function(type) {
      console.log('进入批量出入库', this.selectedRowKeys)
      if (!this.url.warehouseEdit) {
        this.$message.error('请设置url.warehouseEdit!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this
        this.$confirm({
          title: '确认批量出入库',
          content: '是否批量出入库?',
          onOk: function() {
            that.loading = true
            getAction(that.url.batchwarehouseEdit, { ids: ids, type: type })
              .then(res => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.loadData()
                  that.onClearSelected()
                } else {
                  that.$message.warning(res.message)
                }
              })
              .finally(() => {
                that.loading = false
              })
          }
        })
      }
    },
    WarehouseEdit(info, type) {
      console.log('info', info)
      console.log('type', type)
      let params = { id: info.id, type: type }
      getAction(this.url.warehouseEdit, params).then(res => {
        if (res.success) {
          this.$message.success('出入库成功！')
        } else {
          this.$message.error('出入库失败')
        }
        this.loadData(this.ipagination.current)
      })
    },
    downloadFile(info, type) {
      const that = this
      window.open(`${window._CONFIG['domianURL']}/${this.url.downloadFile}` + '?id=' + info.id + '&type=' + type)
    },
    hideModal() {
      //window.print()
      let subOutputRankPrint = document.getElementById('print')
      let newContent = subOutputRankPrint.innerHTML
      let oldContent = document.body.innerHTML

      document.body.innerHTML = newContent
      window.print()
      window.location.reload()
      document.body.innerHTML = oldContent

      this.visible = false
    },
    success(text) {
      this.visible = true
      this.imgUrl = `${window._CONFIG['domianURL']}/${this.url.getQrCode}?id=${text}`
    },
    GetQrCode(text) {
      return `${window._CONFIG['domianURL']}/${this.url.getQrCode}?id=${text}`
      //return 'https://static.jeecg.com/upload/test/3a4490d5d1cd495b826e528537a47cc1.jpg'
    },
    initDictConfig() {}
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>